package com.eastfair.venueservice.service.impl;

import com.eastfair.auth.api.AuthServiceFeign;
import com.eastfair.auth.api.GlobalBusinessServiceFeign;
import com.eastfair.auth.api.RoleServiceFeign;
import com.eastfair.auth.api.UserAccountServiceFeign;
import com.eastfair.auth.dto.RoleDTO;
import com.eastfair.auth.dto.UserAccountDto;
import com.eastfair.auth.dto.UserRoleDto;
import com.eastfair.auth.entity.Role;
import com.eastfair.auth.enumeration.RoleRoleTypeEnum;
import com.eastfair.auth.vo.RoleVo;
import com.eastfair.auth.vo.UserAccountVo;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.venueservice.dto.RegisterDTO;
import com.eastfair.venueservice.enumeration.RegisterRoleEnum;
import com.eastfair.venueservice.exceptioncode.VenueServiceExceptionCode;
import com.eastfair.venueservice.service.RegisterService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 业务实现类
 * 服务商注册
 * </p>
 *
 * @author lyw
 * @date 2022-07-28
 */
@Slf4j
@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserAccountServiceFeign userAccountServiceFeign;
    @Autowired
    private AuthServiceFeign authServiceFeign;
    @Autowired
    private RoleServiceFeign roleServiceFeign;
    @Autowired
    private GlobalBusinessServiceFeign globalBusinessServiceFeign;

    /**
     * 服务商注册
     * @param registerDTO
     * @return
     */
    @Override
    public UserAccountVo registerSubSystemUser(RegisterDTO registerDTO) {
        R<UserAccountVo> r =userAccountServiceFeign.registerSubSystemUser(registerDTO.getUserAccountDto());
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }

        //角色字典值
        RegisterRoleEnum registerRole = RegisterService.getRegisterRole(registerDTO.getServiceProviderType());
        if (registerRole == null) {
            throw BizException.wrap(VenueServiceExceptionCode.SERVICE_PROVIDER_TYPE_ERROR);
        }
        RoleDTO roleDTO =new RoleDTO();
//        roleDTO.setBusinessTag(registerRole.getCode());
        roleDTO.setRoleType(RoleRoleTypeEnum.P_VENUE_ADMIN);
        //查询基础角色信息
        R<List<RoleVo>> queryRole = globalBusinessServiceFeign.queryRole(roleDTO);
        if(!queryRole.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        //判断角色菜单不能为空
        if(Objects.nonNull(queryRole) && Objects.nonNull(queryRole.getData()) && Objects.nonNull(queryRole.getData().get(0).getMenuIds()) && queryRole.getData().get(0).getMenuIds().size()>0){
            throw BizException.wrap(VenueServiceExceptionCode.NOT_ROLE_MENU_NULL);

        }

        //创建用户角色
        RoleDTO role =new RoleDTO();
        role.setMenuIds(queryRole.getData().get(0).getMenuIds());
        role.setBusinessTag(registerRole.getCode());
        R<Role> roleR = roleServiceFeign.createRole(role);
        if(!roleR.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }

        //角色id集合
        List<Long> roleIds=Lists.newArrayList();
        roleIds.add(roleR.getData().getId());
        List<Long> userIds = Lists.newArrayList();
        userIds.add(r.getData().getId());
        UserRoleDto userRoleDto =new UserRoleDto();
        userRoleDto.setRoleIds(roleIds);
        userRoleDto.setUserIds(userIds);
        //给用户添加基础角色
        R userRole = authServiceFeign.addUserRole(userRoleDto);
        if(!userRole.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }

        //修改关联信息
        UserAccountDto userAccountDto=new UserAccountDto();
        userAccountDto.setId(r.getData().getId());
        userAccountDto.setMatingId(null);
        R<Boolean> userAccountR = userAccountServiceFeign.updateCorrelationById(userAccountDto);
        if(!userAccountR.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }
}
